package cn.botter.subject.application.controller;

import cn.botter.subject.application.convert.SubjectCategoryDTOConverter;
import cn.botter.subject.application.convert.SubjectLabelDTOConverter;
import cn.botter.subject.application.dto.SubjectCategoryDTO;
import cn.botter.subject.application.dto.SubjectLabelDTO;
import cn.botter.subject.common.entity.Result;
import cn.botter.subject.common.utils.LoginUtil;
import cn.botter.subject.domain.entity.SubjectCategoryBO;
import cn.botter.subject.domain.service.SubjectDomainCategoryService;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {

    @Resource
    private SubjectDomainCategoryService subjectDomainCategoryService;

    /**
     * 添加分类的接口
     *
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectCategoryDTO));

            }

            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryName(), "分类类型不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "分类父id不能为空");
//            Preconditions.checkArgument(StringUtils.isEmpty(subjectCategoryDTO.getCategoryName()) , "分类名不能为空");
//            Preconditions.checkArgument(subjectCategoryDTO.getCategoryName().isEmpty() , "分类名不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryName(), "分类名不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertBoToCategory(subjectCategoryDTO);
            subjectDomainCategoryService.add(subjectCategoryBO);
            return Result.OK(true);
        } catch (Exception e) {
            log.info("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.FAILED(e.getMessage());
        }

    }

    /**
     * 查询岗位大类
     *
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {

            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDtoToCategoryBO(subjectCategoryDTO);
            List<SubjectCategoryBO> boList = subjectDomainCategoryService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE.
                    convertBoToCategoryDTOList(boList);
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryPrimaryCategory.dto:{}", JSON.toJSONString(subjectCategoryDTOList));

            }
            return Result.OK(subjectCategoryDTOList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.FAILED("查询失败");
        }
    }

    /**
     * 根据分类id查询二级分类
     *
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryCategoryByPrimary.dto:{}", JSON.toJSONString(subjectCategoryDTO));

            }
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "分类父id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDtoToCategoryBO(subjectCategoryDTO);
            List<SubjectCategoryBO> boList = subjectDomainCategoryService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE.
                    convertBoToCategoryDTOList(boList);
            return Result.OK(subjectCategoryDTOList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.FAILED("查询失败");
        }
    }


    /**
     * 更新分类
     *
     * @return Boolean类型的结果
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.update.dto:{}",
                        JSON.toJSONString(subjectCategoryDTO));

            }
            Preconditions.checkNotNull(subjectCategoryDTO.getId(), "id不能为空,id为空更新数据失败");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDtoToCategoryBO(subjectCategoryDTO);
            Boolean result = subjectDomainCategoryService.update(subjectCategoryBO);
            if (result) {
                return Result.OK(result);
            }
            return Result.RESET(false, 520, "id不存在，请重新检查", null);

        } catch (Exception e) {
            log.error("SubjectCategoryController.update.error:{}", e.getMessage(), e);
            return Result.FAILED("查询失败");
        }
    }

    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.delete.dto:{}",
                        JSON.toJSONString(subjectCategoryDTO));

            }
            Preconditions.checkNotNull(subjectCategoryDTO.getId(), "id不能为空,id为空删除数据失败");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDtoToCategoryBO(subjectCategoryDTO);
            Boolean result = subjectDomainCategoryService.delete(subjectCategoryBO);

            if (result) {
                return Result.OK(result);
            }
            return Result.RESET(false, 520, "id不存在，请重新检查", null);
        } catch (Exception e) {
            log.error("SubjectCategoryController.delete.error:{}", e.getMessage(), e);
            return Result.FAILED("删除失败");
        }
    }

    /**
     * 一次性查询Category和Label
     *
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/queryCategoryAndLabel")
    public Result<List<SubjectCategoryDTO>> queryCategoryAndLabel(@RequestBody SubjectCategoryDTO subjectCategoryDTO,
                                                                  HttpServletRequest request) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryCategoryAndLabel.dto:{}",
                        JSON.toJSONString(subjectCategoryDTO));
            }
            if (log.isInfoEnabled()) {
                log.info("request:{}", request);
            }

            String loginId = LoginUtil.getLoginId("loginId");
            Preconditions.checkNotNull(subjectCategoryDTO.getId(), "id不能为空！");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDtoToCategoryBO(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectDomainCategoryService.queryCategoryAndLabel(subjectCategoryBO);
            List<SubjectCategoryDTO> dtoList = new LinkedList<>();
            subjectCategoryBOList.forEach(bo -> {
                SubjectCategoryDTO dto = SubjectCategoryDTOConverter.INSTANCE.convertBoToDto(bo);
                List<SubjectLabelDTO> lableDtoList = SubjectLabelDTOConverter.INSTANCE.convertBoToDTOList(bo.getLabelBOList());
                dto.setLabelDTOList(lableDtoList);
                dtoList.add(dto);
            });
            return Result.OK(dtoList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.delete.error:{}", e.getMessage(), e);
            return Result.FAILED("删除失败");
        }
    }
}
